<template>
  <div class="header">
        <div class="page-title" style="margin-bottom:20px">表单管理</div>
        <el-select v-model="searchForm.branch" @change="branchChange" v-show="isShowBranchSelect">
          <el-option label="所有分会" value=""></el-option>
          <el-option
              v-for="item in branchsSearch"
              :key="item.name"
              :label="item.name"
              :value="item.name"
          >
          </el-option>
        </el-select>
        <el-button type="primary" style="margin-left:20px" @click="handleExport">导出</el-button>
    <el-table :data="tableData" class="m-t-5">
      <el-table-column type="selection" width="55"></el-table-column>
      <el-table-column prop="id" label="id" width="100"></el-table-column>
      <el-table-column prop="wxname" label="微信名"></el-table-column>
      <el-table-column prop="wxname" label="微信头像">
       <template #default="v">
          <el-image :src="v.row.wximage" fit="cover"></el-image>
        </template>
      </el-table-column>
      <el-table-column prop="name" label="姓名"></el-table-column>
      <el-table-column prop="sex" label="性别"></el-table-column>
      <el-table-column prop="idcard" label="身份证"></el-table-column>
      <el-table-column prop="phone" label="电话"></el-table-column>
      <el-table-column prop="department" label="科室"></el-table-column>
      <el-table-column prop="work" label="职务"></el-table-column>
      <el-table-column prop="company" label="公司"></el-table-column>
      <el-table-column prop="branch" label="分会"></el-table-column>
      <el-table-column prop="branch_office" label="分会职务"></el-table-column>
      <el-table-column prop="create_time" label="提交时间"></el-table-column>
      <el-table-column label="操作" min-width="200">
        <template #default="v">
          <el-button
            type="primary"
            size="mini"
            icon="el-icon-edit"
            class="blue-btn"
            @click="handleEdit(v.row)"
            >编辑
          </el-button>
          <el-button
            type="danger"
            size="mini"
            icon="el-icon-delete"
            @click="handleDelete(v.row)"
            >删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      background
      hide-on-single-page
      layout="prev, pager, next"
      :total="total"
      :current-page="current"
      @current-change="changePage"
    >
    </el-pagination>
  </div>

  <el-dialog title="报名记录操作" v-model="dialogFormVisible" width="35%">
    <el-form>
      <el-form-item label="姓名">
        <el-input
          v-model="form.name"
          autocomplete="off"
          placeholder="请输入名称"
        ></el-input>
      </el-form-item>
      <el-form-item label="性别">
        <el-select v-model="form.sex" placeholder="请选择">
          <el-option
            v-for="item in sexs"
            :key="item"
            :label="item"
            :value="item"
          >
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="身份证号">
        <el-input
          v-model="form.idcard"
          autocomplete="off"
          placeholder="请输入身份证号"
        ></el-input>
      </el-form-item>
      <el-form-item label="电话">
        <el-input
          v-model="form.phone"
          autocomplete="off"
          placeholder="请输入电话"
        ></el-input>
      </el-form-item>
      <el-form-item label="科室">
        <el-input
          v-model="form.department"
          autocomplete="off"
          placeholder="请输入科室"
        ></el-input>
      </el-form-item>
      <el-form-item label="职务">
        <el-input
          v-model="form.work"
          autocomplete="off"
          placeholder="请输入职务"
        ></el-input>
      </el-form-item>
      <el-form-item label="公司">
        <el-input
          v-model="form.company"
          autocomplete="off"
          placeholder="请输入公司"
        ></el-input>
      </el-form-item>
      <el-form-item label="分会">
        <el-select v-model="form.branch">
          <el-option label="请选择" value=""></el-option>
          <el-option
            v-for="item in branchs"
            :key="item.name"
            :label="item.name"
            :value="item.name"
          >
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="分会职务">
        <el-input
          v-model="form.branch_office"
          autocomplete="off"
          placeholder="请输入分会职务"
        ></el-input>
      </el-form-item>
    </el-form>

    <template v-slot:footer>
      <div class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="handleSubmitForm">确 定</el-button>
      </div>
    </template>
  </el-dialog>
</template>
<script>
import {
  request_form_list,
  request_form_create,
  request_form_update,
  request_form_delete,
  request_getbranchs,
  request_user_info,
  request_download_form,
} from "@/util/api";

export default {
  name: "SeekType",
  data() {
    return {
      current: 1,
      total: 0,
      form: [],
      dialogFormVisible: false,
      actionType: 0, // 1 添加  2 编辑
      sexs: ["男", "女"],
      branchs: [],
      branchsSearch: [],
      searchForm: {
        branch: ''
      },
      tableData: [],
      isShowBranchSelect: false
    };
  },
  computed: {},
  mounted() {
    this.init();
    request_user_info()
      .then((res) => {
        if (res.data.data.branch.length == 0) {
            this.isShowBranchSelect = true
        }
        this.setMenuPosition();
      })
  },
  methods: {
    branchChange(value){
      this.searchForm.branch=value;
      this.request_form_list();
    },
    init() {
        this.request_form_list();
        request_getbranchs().then((res) => {
          const result = res.data;
          if (result.code == 200) {
            this.branchs = result.data;
            this.branchsSearch = result.data;
          }
        })
    },
    handleExport(){
        this.request_form_list();
        request_download_form({
           branch:this.searchForm.branch,
           is_export:1
        })
    },
    request_form_list(){
        request_form_list({
           branch:this.searchForm.branch,
        })
          .then((res) => {
            const result = res.data;
            if (result.code == 200) {
              this.$nextTick(() => {
                this.tableData = result.data.data;
                this.total = result.data.total;
                this.current = result.data.current;
              })
            }
          })
          .catch();
    },
    // 添加
    handleCCreate() {
      this.actionType = 1;
      this.form = [];
      this.dialogFormVisible = true;
    },
    // 编辑
    handleEdit(row) {
      this.actionType = 2;
      this.form = row;
      this.dialogFormVisible = true;
    },
    // 提交
    handleSubmitForm() {
      let apiFunc;
      switch (this.actionType) {
        case 1: // 添加
          apiFunc = request_form_create;
          break;
        case 2: // 编辑
          apiFunc = request_form_update;
          break;
        default:
          this.$message.error({
            message: "操作类型不正确",
            offset: 70,
          });
          return false;
      }
      apiFunc({
        ...this.form,
      }).then((res) => {
        const result = res.data;
        if (result.code == 200) {
          this.dialogFormVisible = false;
          this.$notify.success({
            title: "系统提示",
            message: result.message,
            offset: 70,
          });
          this.changePage(1);
        } else {
          this.$message.error({
            message: result.message,
            offset: 70,
          });
        }
      });
    },
    // 删除
    handleDelete(row) {
      this.$confirm("此操作将永久删除，是否继续？", "系统提示", {
        type: "warning",
        confirmButtonText: "确定",
        cancelButtonText: "取消",
      })
        .then(() => {
          request_form_delete({
            id: row.id,
          })
            .then((res) => {
              const result = res.data;
              if (result.code == 200) {
                this.$message.success({
                  message: "删除成功！",
                  offset: 70,
                });
                this.changePage(this.page);
              } else {
                this.$message.error({
                  message: result.message,
                  offset: 70,
                });
              }
            })
            .catch();
        })
        .catch(() => {
          this.$message.info({
            message: "已取消删除！",
            offset: 70,
          });
        });
    },
    // 分页
    changePage(page) {
      request_form_list({
        page,
      })
        .then((res) => {
          const result = res.data;
          if (result.code == 200) {
            this.tableData = result.data.data;
            this.total = result.data.total;
            this.current = result.data.current;
          }
        })
        .catch();
    },
  },
};
</script>

<style scoped  lang="less">
</style>